﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace ArduinoSketch.Core
{
    
    public partial class ArduinoTarget : UserControl
    {
       
        public ArduinoTarget()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            LoadArduinoTargetConfig();
        }

        private void LoadArduinoTargetConfig()
        {
            ArduinoTargetConfig _arduinoTarget = null;

            if (File.Exists(StringConst.ARDU_TARGET_CNFG))
            {
                _arduinoTarget = XmlSerializable.ReadFile<ArduinoTargetConfig>(StringConst.ARDU_TARGET_CNFG);
                if (_arduinoTarget != null)
                {
                    this.txtSelectArduinoDir.Text = _arduinoTarget.ArduinoDirectory;
                    this.txtSelectArduinoLibDir.Text = _arduinoTarget.ArduinoLibDirectory;
                }
            }          
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            ArduinoTargetConfig _arduinoTarget = new ArduinoTargetConfig();
            _arduinoTarget.ArduinoDirectory = this.txtSelectArduinoDir.Text;
            _arduinoTarget.ArduinoLibDirectory = this.txtSelectArduinoLibDir.Text;


            XmlSerializable.WriteFile<ArduinoTargetConfig>(_arduinoTarget, StringConst.ARDU_TARGET_CNFG);
        }

        private void btnSelectArduinoDir_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtSelectArduinoDir.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnSelectArduinoLibDir_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtSelectArduinoLibDir.Text = folderBrowserDialog.SelectedPath;
            }
        }
    }

    [Serializable]
    [XmlRoot("ArduinoTarget")]
    public class ArduinoTargetConfig
    {
        public ArduinoTargetConfig()
        {
            ArduinoDirectory = String.Empty;
            ArduinoLibDirectory = String.Empty;
        }

        [XmlElement()]
        public string ArduinoDirectory { get; set; }

        [XmlElement()]
        public string ArduinoLibDirectory { get; set; }
    }

}
